package model.workersWithData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.apache.log4j.Logger;

import entities.IGarage;
import entities.IMaster;

public class WorkerWithMaster {

	private static final String MASTER = "Master ";
	private static final String PERFORMS = " performs order #";
	private static final String BUSY = " busy";
	private static final String FREE = " free";
	private static final String MAS_EXISTS = "Sorry, master with this name already exists";
	private static final String MAS_ADD = "Thanks,master was add";
	private static final String MAS_DELETE = "This master was deleted";
	private static final String MAS_NOT_EXISTS = "Sorry master with this name is absent";
	private static final Logger log = Logger.getLogger(WorkerWithMaster.class);

	public static void getOrdersByMaster(String nameMaster,
			ArrayList<IMaster> masList) {
		try {
			for (IMaster m : masList) {
				if (m.getNameMaster().equals(nameMaster)) {
					System.out.println(MASTER + m.getNameMaster() + PERFORMS
							+ m.getOrder().getNumberOrder());
				}
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	public static void SortMaster(ArrayList<IMaster> masterList,
			Comparator sortBy) {
		try {
			Collections.sort(masterList, sortBy);
			for (IMaster master : masterList) {
				if (master.getEmploymentMaster()) {
					System.out.println(MASTER + master.getNameMaster() + BUSY);
				} else {
					System.out.println(MASTER + master.getNameMaster() + FREE);
				}
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}

	public static void addMaster(ArrayList<IMaster> masterList, IMaster master) {
		try {
			int i = 0;
			for (IMaster m : masterList) {
				if (m.getNameMaster().equals(master.getNameMaster())) {
					System.out.println(MAS_EXISTS);
					i++;
				}
			}
			if (i == 0) {
				masterList.add(master);
				System.out.println(MAS_ADD);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}

	}

	public static void deleteMaster(ArrayList<IMaster> masterList,
			String masterName) {
		try {
			ArrayList<IMaster> temp = new ArrayList<IMaster>();
			for (IMaster mas : masterList) {
				if (!mas.getNameMaster().equals(masterName)) {
					temp.add(mas);
				}
			}
			if (temp.size() == masterList.size()) {
				System.out.println(MAS_NOT_EXISTS);
			} else {
				masterList.clear();
				masterList.addAll(temp);

				System.out.println(MAS_DELETE);
			}
		} catch (NullPointerException n) {
			log.warn("Collection is empty");

		}
	}
	
	

}
